define(function(require,exports,module) {
    var myPub = require('./public'); 
    
	$(function() {
	    var oFooter = $('.js_footer');              //底部
	    var oLoadingPage =$('.js_loadingPage');
	    
	    var oDefaultAddr = $('.js_DefaultAddr');	//默认地址选择 
	    var oClearBtn = $('.js_clearBtn');		    //清除按钮
	
		var oUserName = $('.js_userName');  	//收货人姓名
		var oUserIphone = $('.js_userIphone');	//收货人电话

        var addressInit = $('.initAddress');
        var provinceInput = $('.js_province');
        var cityInput = $('.js_city');
        var areaInput = $('.js_area');
		var addressInput = $('.js_address');
        var saveBtn = $('.js_saveBtn');
		var oldAddress = $('.jsOldDetailAddress');

		var userDetailAddress = $('.jsUserDetailAddress');

    	oFooter.on("touchmove",function(e) {e.preventDefault()});
	    oLoadingPage.on('touchmove',function() {return false});

		var streetName = "";
		var addressName = "";

		var oldAddressVal = oldAddress.val();
		if(oldAddressVal != null && oldAddressVal.length > 0){
			var index = oldAddressVal.indexOf(" ");
			streetName = oldAddressVal.substring(0,index);
			addressName = oldAddressVal.substring(index + 1,oldAddressVal.length);

			// $('.js_cityStreetResult').val(streetName);
            userDetailAddress.val(addressName);
		}
	    
	    
	    
	    //默认地址选择
	    oDefaultAddr.hammer().on('tap',function() {
	    	//关闭mui tap 事件
	    	myPub.OffMuiEvent(false)
	    	if(oDefaultAddr.hasClass('active')) {
	    		oDefaultAddr.removeClass('active')
	    	}
	    	else {
	    		oDefaultAddr.addClass('active')
	    	}
	    });

	    
	    //收货人电话检测
		oUserIphone.on('input propertychange', function() {
			oUserIphone.siblings('.js_clearBtn').show();
			
			myPub.checkMobile(oUserIphone,function() {
				myPub.TipLayer('收货人手机号码不能为空！');
				oUserIphone.parents('.js_userAddrInfo').removeClass('has_error').removeClass('has_success');
				oUserIphone.siblings('.js_clearBtn').hide();
				oUserIphone.attr('judgeBloor',false)
			},function() {
				oUserIphone.parents('.js_userAddrInfo').addClass('has_error').removeClass('has_success')
				oUserIphone.attr('judgeBloor',false)
			},function() {
				oUserIphone.parents('.js_userAddrInfo').addClass('has_success').removeClass('has_error');
				oUserIphone.attr('judgeBloor',true)
			});
			
		})
	
		//收货人姓名检测
		oUserName.on('input propertychange', function() {
			oUserName.siblings('.js_clearBtn').show();
			
			myPub.checkUserName(oUserName,function() {
				myPub.TipLayer('收货人姓名不能为空！');
				oUserName.parents('.js_userAddrInfo').removeClass('has_error').removeClass('has_success');
				oUserName.siblings('.js_clearBtn').hide();
				oUserName.attr('judgeBloor',false)
			},function() {
				oUserName.parents('.js_userAddrInfo').addClass('has_error').removeClass('has_success')
				oUserName.attr('judgeBloor',false)
			},function() {
				oUserName.parents('.js_userAddrInfo').addClass('has_success').removeClass('has_error');
				oUserName.attr('judgeBloor',true)
			});
			
		});

        saveBtn.on('click',function(){
            if($.trim(oUserName.val()) == ""){
                myPub.TipLayer("请输入收货人姓名");
                return false;
            }
            if(oUserName.parents('.js_userAddrInfo').hasClass('has_error')){
                myPub.TipLayer("请输入正确的姓名");
                return false;
            }
            if($.trim(oUserIphone.val()) == ""){
                myPub.TipLayer("请输入正确的手机号码");
                return false;
            }
            if($('.js_cityResult').val() == ""){
                myPub.TipLayer("请选择所在地区信息");
                return false;
            }
            if($.trim($('textarea[name="userAddress"]').val()) == ""){
                myPub.TipLayer("请填写详细地址信息");
                return false;
            }
            var reg = /^(1[3|4|5|6|7|8])[\d]{9}$/;   //验证手机号码
            if(!reg.test(oUserIphone.val()) ){
                myPub.TipLayer("请输入正确的手机号码");
                return false;
            }
            // 设置默认地址
            if($('.js_DefaultAddr').hasClass('active')){
                $('.js_default').val('true');
            }else{
                $('.js_default').val('false');
            }
            var sStreet = $('.js_cityStreetResult').val();
			// console.log(sStreet);
			addressInput.val(sStreet + " " + userDetailAddress.val());
            $('.js_address_form').submit();
        });

        MuiSeletCity();
        var time_city = null;
        var time_area = null;
        var time_province = null;
        var time_street = null;
        var time_CityData = null;
        var cityPicker, cityStreetPicker;

	    //选择城市
        function MuiSeletCity() {

            (function($$) {
                // $$.init();
                if(cityPicker != undefined) {
                    cityPicker.dispose();
                    cityStreetPicker.dispose();
                }
                $$.ready(function() {
                    cityPicker = new $$.PopPicker({  layer: 3  });
                    cityStreetPicker = new $$.PopPicker({  layer: 1  });

                    cityPicker.setData(cityData3);

                    var showCityPickerButton = $('.js_showCityBtn')[0];
                    var showCityPickerStreetButton = $('.js_showCityStreetBtn')[0];
                    var cityResult = $('.js_cityResult');
                    var cityStreetResult = $('.js_cityStreetResult');
                    var oPicker = $('.mui-poppicker .mui-picker');

                    if(cityResult.length < 1) return
                    //有地址
                    cityResult.val().trim() !== '' ? OldCitySet(cityResult, cityPicker, cityStreetPicker, cityStreetResult) : cityResult.val('');

                    showCityPickerButton.addEventListener('click', function(event) {
                        //开启mui tap 事件
                        myPub.OffMuiEvent(true);
                        cityPicker.show(function(items) {
                            var province = ( items[0] || {}).text;
                            var city = ( items[1] || {}).text;
                            var area = ( items[2] || {}).text;


                            if(area == undefined) {area = ''}
                            var _seletCity = province + " " + city + " " + area;
                            cityResult[0].value = _seletCity;

                            cityStreetPicker.setData(items[2].children);
                            cityStreetResult[0].value = '';

                            //城市 省份 地区
                            provinceInput.val(province);
                            cityInput.val(city);
                            areaInput.val(area);

                        });
                    }, false);


                    showCityPickerStreetButton.addEventListener('click', function(event) {
                        //开启mui tap 事件
                        myPub.OffMuiEvent(true);
                        if(cityResult[0].value == '') {
                            myPub.TipLayer('请先选择地区！');
                        }
                        else{
                            cityStreetPicker.show(function(items) {
                                var sStreet = ( items[0] || {}).text;
                                if(sStreet == undefined) {sStreet=''}
                                cityStreetResult[0].value = sStreet;
                            });

                        }
                    }, false);
                    if(addressInit.length > 0){
                        cityResult[0].value = addressInit.text();
                    }


                });
            })(mui);
        }

        exports.MuiSeletCity = MuiSeletCity;

        // 旧地址数据定位设置
        function OldCitySet(cityResult, cityPicker, cityStreetPicker, cityStreetResult) {
            var _OldAddr = cityResult.val().split(' ');
            var get_firstName = _OldAddr[0].substr(0,_OldAddr[0].length-1);
            var get_secondName = _OldAddr[1].substr(0,_OldAddr[1].length-1);
            var get_threeName = _OldAddr[2].substr(0,_OldAddr[2].length-1);

            var Off_firstName = false;
            var Off_secondName = false;
            var Off_threeName = false;

            var date_prov = cityPicker.pickers[0].items;
            clearTimeout(time_city);
            clearTimeout(time_area);
            clearTimeout(time_province);
            clearTimeout(time_street);
            clearTimeout(time_CityData);

            time_province = setTimeout(setProv, 40);
            time_city = setTimeout(setCity, 60);
            time_area = setTimeout(setArea, 80);
            time_CityData = setTimeout(setCityData, 100);
            time_street = setTimeout(setSteets, 120);


            function setProv(fn) {
                for (var i=0; i<date_prov.length; i++) {
                    var _provName = date_prov[i].text.substr(0,date_prov[i].text.length-1)
                    if(_provName == get_firstName) {
                        // cityPicker.pickers[0].setAngle(cityPicker.pickers[0].elementItems[i].angle);
                        cityPicker.pickers[0].setSelectedIndex(i);
                        Off_firstName = true;
                        if(fn)fn();
                    }
                }
            }

            function setCity(fn) {
                var date_city = cityPicker.pickers[1].items;
                for (var i=0; i<date_city.length; i++) {
                    var _cityName = date_city[i].text.substr(0,date_city[i].text.length-1)
                    if(_cityName == get_secondName) {
                        cityPicker.pickers[1].setSelectedIndex(i);
                        Off_secondName = true;
                        if(fn)(fn);
                    }
                }
            }

            function setArea(fn) {
                var date_area = cityPicker.pickers[2].items;
                for (var i=0; i<date_area.length; i++) {
                    var _areaName = date_area[i].text.substr(0,date_area[i].text.length-1)
                    if(_areaName == get_threeName) {
                        cityPicker.pickers[2].setSelectedIndex(i);
                        Off_threeName = true;
                        if(fn)(fn);
                    }
                }
            }

            function setSteets(fn) {
                var date_street = cityStreetPicker.pickers[0].items;
                var flag = false;
                for (var i=0; i<date_street.length; i++) {
                    var _streetName = date_street[i].text;
                    // console.log($('.js_cityStreetResult').val() + "+   aaa");
                    if(_streetName == streetName) {
                        // cityStreetPicker.pickers[0].setAngle(cityStreetPicker.pickers[0].elementItems[i].angle);
                        cityStreetPicker.pickers[0].setSelectedIndex(i);
                        cityStreetResult.val(_streetName);
                        flag = true;

                        if(fn)fn();
                    }
                }
                // if(flag == false){
                //     if(streetName != null && streetName.length > 0){
                //         $('.jsUserDetailAddress').val(streetName + " " +  addressName);
                //     }
                // }
            }

            function setCityData () {
                cityPicker.callback = function(items) {
                    if(!Off_threeName || !Off_firstName || !Off_secondName) {
                        cityResult.val('');
                        // cityStreetResult.val('');

                        provinceInput.val('');
                        cityInput.val('');
                        areaInput.val('');
                        return
                    }


                    var province = ( items[0] || {}).text;
                    var city = ( items[1] || {}).text;
                    var area = ( items[2] || {}).text;

                    if(area == undefined) {area = ''}
                    var _seletCity = province + " " + city + " " + area;
                    cityResult[0].value = _seletCity;
                    cityStreetPicker.setData(items[2].children);
                    cityStreetResult[0].value = '';
                    //城市 省份 地区
                    provinceInput.val(province);
                    cityInput.val(city);
                    areaInput.val(area);

                }
                mui.trigger(cityPicker.ok,'tap');
            }

        }


  	})
  	
  	
    
  	
});


